package com.xintonghua.bussiness.ui.fragment.client;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.SourceBean;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.DatePicker;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * Created by liqi on 2018/3/30.
 * 添加成员
 */
public class AddCustomersActivity extends BaseActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_sex)
    TextView etSex;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.tv_from)
    TextView tvFrom;
    List<SourceBean> list;
    private int sourceId = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_customers);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("添加客户");
        etSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JumpUtils.jumpActivityForResult(AddCustomersActivity.this, SelectsSexActivity.class, null, MyState.requestTwo);
            }
        });
        httpCent.getResouceList(this, MyState.requestTwo);
    }

    @OnClick({R.id.tv_birthday, R.id.tv_from})
    public void onViewClicked1(View view) {
        switch (view.getId()) {
            case R.id.tv_birthday:
                onYearMonthDayPicker(view);
                break;
            case R.id.tv_from:
                showForm();
                break;
        }

    }

    public void onYearMonthDayPicker(View view) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        final DatePicker picker = new DatePicker(AddCustomersActivity.this);
        picker.setCanLoop(true);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(1790, 8, 29);
        picker.setRangeEnd(2111, 1, 11);
        picker.setSelectedItem(year, month, day);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                tvBirthday.setText(year + "-" + month + "-" + day);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {

            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

    private void showForm() {
        final List<String> lists = new ArrayList<>();
        for (SourceBean str : list) {
            lists.add(str.getResource());
        }
        SinglePicker<String> picker = new SinglePicker<>(this, lists);
        picker.setCanLoop(false);//不禁用循环
        picker.setLineVisible(true);
        picker.setShadowVisible(true);
        picker.setTextSize(18);
        picker.setSelectedIndex(1);
        picker.setWheelModeEnable(true);
        //启用权重 setWeightWidth 才起作用
        picker.setLabel(null);
        picker.setWeightEnable(true);
        picker.setWeightWidth(1);
        picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
        picker.setUnSelectedTextColor(0xFF999999);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                for (SourceBean bean : list) {
                    if (item.equals(bean.getResource())) {
                        sourceId = bean.getId();
                    }
                }
                tvFrom.setText(item);
            }
        });
        picker.show();
    }

    @OnClick(R.id.btn_add)
    public void onViewClicked() {

        if (TextUtils.isEmpty(etName.getText().toString().trim())) {
            MyUtils.mToast(AddCustomersActivity.this, "请输入姓名！");
            return;
        }
        if (TextUtils.isEmpty(etPhone.getText().toString().trim())) {
            MyUtils.mToast(AddCustomersActivity.this, "请输入电话！");
            return;
        }
        if (TextUtils.isEmpty(etSex.getText().toString().trim())) {
            MyUtils.mToast(AddCustomersActivity.this, "请选择性别！");
            return;
        }
        if (TextUtils.isEmpty(tvBirthday.getText().toString().trim())) {
            MyUtils.mToast(AddCustomersActivity.this, "请选择生日！");
            return;
        }
        if (sourceId == 0) {
            mToast("请选择客户来源");
            return;
        }
        HttpCent.getInstance(AddCustomersActivity.this).addUserSave(etPhone.getText().toString().trim(), etName.getText().toString().trim(), sextype + "", "" + tvBirthday.getText().toString(), sourceId, AddCustomersActivity.this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                try {
                    MyUtils.mToast(AddCustomersActivity.this, "添加成功!");
                    Intent mIntent = new Intent(MyState.BROAD_CASET_RECEIVER_REFRESH_CLIENT);
//                        mIntent.putExtra("message", "hahahahaha");
                    sendBroadcast(mIntent);


                    finish();
                } catch (Exception e) {
                }
                break;
            case MyState.requestTwo:
                list = JSONObject.parseArray((String) data, SourceBean.class);
                break;
        }
    }

    int sextype = -1;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyState.requestTwo && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                sextype = data.getIntExtra("sex", 2);
                switch (sextype) {
                    case 0:
                        etSex.setText("男");
                        break;
                    case 1:
                        etSex.setText("女");
                        break;

                }


            }
        }
    }


}
